/*
 *功能：解析Json字符串
 *{
 *  "sensor": "gps",
 *  "time": 1351824120,
 *  "data": [
 *     48.756080,
 *     2.302038
 *   ]
 * }
 *  char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
 */
/*
 //开始获得每个对象
  const char* sensor = root["sensor"];
  long time = root["time"];
  double latitude = root["data"][0];
  double longitude = root["data"][1];
*/

 
#include <ArduinoJson.h>
 
void setup() {
  Serial.begin(9600);
  while (!Serial) {
    // wait serial port initialization
  }
 
  // Json对象对象树的内存工具 静态buffer 200 是大小 如果这个Json对象更加复杂，那么就需要根据需要去增加这个值.
  StaticJsonBuffer<200> jsonBuffer;
  // JSON input string.
  // It's better to use a char[] as shown here. If you use a const char* or a String, ArduinoJson will have to make a copy of the input in the JsonBuffer.
  char json[] ="{\"datastreams\":[{\"id\":\"hum\",\"datapoints\":[{\"value\":16.0}]}]}";
  
  //创建根，也就是顶节点 然后把Json字符串扔进去  由于是对象 所以我们用parseObject
  //如果是数组 用 JsonArray& root = jsonBuffer.parseArray(json);
   JsonObject& root = jsonBuffer.parseObject(json);
 
  // 判断是否解析成功 (Important)
  if (!root.success()) {
    Serial.println("parseObject() failed");
    return;
  }
 
  //开始获得每个对象
  const char* datastreams = root["datastreams"][0];                 //不知为何不显示
  char* id = root["datastreams"][0]["id"];
  char* datapoints = root["datastreams"][0]["datapoints"];   //数组对象采用下表的
  double value = root["datastreams"][0]["datapoints"][0]["value"];
 
  // Print values.
  Serial.println(datastreams);
  Serial.print("id：");
  Serial.println(id);
  Serial.println(datapoints);
  Serial.print("value：");
  Serial.println(value,2);
}
 
void loop() {
  // not used in this example
}
